/**
* @version		$Id: slg.vm.tabs.js r5 2010-10-24 imranweb7 $
* @copyright	Copyright (C) 2010 imranweb7.com. All rights reserved.
* @license		GNU/GPL
* @Author:      Muhammed Imran Hussain
* @Websites:    http://www.imranweb7.com

* mod_slg_vm_productslider! is a free Joomla+Virtuemart module.
* This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/

var SlgVmTabs = new Class({

    Implements: [Options, Events],

    options: {
        startIndex: 0,
        activeClass: 'active',
		tabGroupWidth: 500,
		tabGroupSeparator: true,
		tabSlidePosition: Array(),
		tabItemMarginLeft: 4,
		tabItemMarginRight: 4,
		firePreviousFn: false,
        fps: 120,
        duration: 1000,
        transition: 'expo:in:out',
        autoPlay: true,
        autoPlayWait: 2000
    },
	

    initialize: function(tabs, contents, options) {
        this.setOptions(options);
		
        this.tabsElement = tabs[0];
        this.contentsElement = contents[0];	
		
		this.tabsList = this.tabsElement.getElements('li');
        this.contentsList = this.contentsElement.getChildren('li');

        this.slideFx = new Fx.Morph(this.contentsElement, {
            fps : this.options.fps,
            duration: this.options.duration,
            transition: this.options.transition
        });
		
		this.slideTabFx = new Fx.Morph(this.tabsElement, {
            fps : this.options.fps,
            duration: this.options.duration,
            transition: this.options.transition
        });

        this.windowWidth = this.contentsList[0].getSize().x;
        this.currentPosition = -(this.options.startIndex * this.windowWidth);
		
        this.contentsElement.setStyle('left', this.currentPosition + 'px');
        this.currentIndex = this.options.startIndex;
        this.tabsCount = this.tabsList.length;

        this.activeTab = this.tabsList[this.currentIndex].addClass(this.options.activeClass);
        this.activeContents = this.contentsList[this.currentIndex];

	    var tmpTotalTabsWidth = 0, tmpTabItemPosition = 0, tmpPerSlideWidth = 0, tmpTabItemWidth = 0, isSlideFxCall = true;
		
	    this.tabsList.each(function(tab, i) {
            tmpTabItemWidth = (tab.getSize().x)+(this.options.tabItemMarginLeft)+(this.options.tabItemMarginRight);
			
			tmpPerSlideWidth += tmpTabItemWidth;

			if(tmpPerSlideWidth > this.options.tabGroupWidth){
				tmpPerSlideWidth = tmpTabItemWidth;
				isSlideFxCall = true;
				
				tmpTabItemPosition = tmpTotalTabsWidth;
				
				if(this.options.tabGroupSeparator){
					tab.setStyle('margin-left', '1000px');
					tmpTabItemWidth += 1000;
					tmpTabItemPosition += 1000;
				}
			}
			
			this.options.tabSlidePosition[i] = Array();
			this.options.tabSlidePosition[i]['position'] = tmpTabItemPosition;			
			this.options.tabSlidePosition[i]['slide_fx_call'] = isSlideFxCall;
			
			isSlideFxCall = false;
			tmpTotalTabsWidth += tmpTabItemWidth;

			this.setupTabs(tab, this.contentsList[i], i);
        }, this);

        if (this.options.autoPlay) {
            this.play();
        }
    },
	

    setupTabs: function(tab, contents, i) {
        tab.addEvent('mousedown', function(e) {
            if (tab != this.activeTab) {
				this.stop().play();
                this.activeTab.removeClass(this.options.activeClass);
                this.activeTab = tab;
                this.activeTab.addClass(this.options.activeClass);

                var d = (i - this.currentIndex) * this.windowWidth;
                this.currentPosition -= d;
					
				if(this.options.tabSlidePosition[i]['slide_fx_call'] || this.options.firePreviousFn){
					
					this.slideTabFx.start({
						left: '-' + (this.options.tabSlidePosition[i]['position']) + 'px'
					});
				}

                this.slideFx.start({
                      left: this.currentPosition + 'px'
                });
				
                this.currentIndex = i;
            }
        }.bind(this));
    },
	
	
	play: function() {
        this.player = this.loadSlgVmNextSlide.periodical(this.options.autoPlayWait, this);
        return this;
    },
	

    stop: function() {
        $clear(this.player);
        return this;
    },
	

    loadSlgVmNextSlide: function() {
        if (this.currentIndex == this.tabsCount-1) {
            this.tabsList[0].fireEvent('mousedown');
        } else {
            this.tabsList[this.currentIndex+1].fireEvent('mousedown');
        }
		
        return this;
    },
	

    loadSlgVmPreviousSlide: function() {
        this.options.firePreviousFn = true;
		
		if (this.currentIndex == 0) this.tabsList[this.tabsCount-1].fireEvent('mousedown');
        else this.tabsList[this.currentIndex-1].fireEvent('mousedown');
		
		this.options.firePreviousFn = false;
		
        return this;
    }

});